package com.nondo.dean.product.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.*;
import com.nondo.dean.product.service.BrandService;
import com.nondo.dean.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@RestController("apiAdminBrand")
@RequestMapping("/{v}/admin/brand")
public class BrandController {
    @Autowired
    BrandService brandService;

    @Autowired
    ProductService productService;

    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Brand>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Brand> query) {
        View<Page<Brand>> view = new View<Page<Brand>>(20000, "获取数据成功");

        Brand brand = query.getData();

        Page<Brand> page = new Page<Brand>(query.getPage(), query.getLimit(), brandService.count(brand));

        List<Brand> gradeList = brandService.getList(query.getOffset(), query.getLimit(), brand);

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }

    /**
     * 添加
     *
     * @param request
     * @param response
     * @param brand
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Brand> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Brand brand, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //检查是否存在重复的数据
            Brand brandCheck = this.brandService.selectByName(brand.getName());
            if (brandCheck != null) {
                return View.ofParamsError("该品牌已经存在，请不要重复添加");
            }
            this.brandService.add(brand);
        }

        return view;
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @param brand
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Brand> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Brand brand, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Brand brandCheck = this.brandService.selectByName(brand.getName());
            if (brandCheck != null && brand.getId().equals(brandCheck.getId())) {
                return View.ofParamsError("已经存在该品牌名字，无法修改为相同的名字");
            }

            this.brandService.update(brand);
        }

        return view;
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param brandId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{brandId}")
    public View<Brand> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable int brandId) {
        View view = new View(20000, "删除成功");

        Brand brand = this.brandService.select(brandId);

        //如果品牌已经被产品关联，则无法被删除
        int checkProduct = this.brandService.checkProduct(brand);
        if (checkProduct > 0) {
            return View.ofParamsError("该品牌已经被产品关联，无法被删除，请删除对应产品关联后方可删除该品牌");
        }

        this.brandService.delete(brand);

        return view;
    }
}
